<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/*
 * Excel library for Code Igniter applications
 * Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
 */

function to_excel($table, $filename='danhsachthuctap') {
	$headers = ''; // just creating the var for field headers to append to below
	$data = ''; // just creating the var for field data to append to below

	$CI = & get_instance();

	// Starting the PHPExcel library
	$CI->load->library('PHPExcel');
	$CI->load->library('PHPExcel/IOFactory');

	$objPHPExcel = new PHPExcel();
	$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

	$objPHPExcel->setActiveSheetIndex(0);
	
	for ($row = 1; $row <= count($table); $row++){
		$rowData = $table[$row-1];
		$col = 0;
		foreach ($rowData as $value) {
			if (strstr($value,'http://')){
				$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, "Download CV");
				$objPHPExcel->getActiveSheet()->getCellByColumnAndRow($col, $row)->getHyperlink()->setUrl($value);
				$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col, $row)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
				$FontColor = new PHPExcel_Style_Color();
				$FontColor->setRGB("0000DD");
				$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col, $row)->getFont()->setColor($FontColor);
			}else{
				$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
			}
			$col++;
		}
	}

	$objPHPExcel->setActiveSheetIndex(0);

	$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
	
	// Sending headers to force the user to download the file

	$file = "uploads/".$filename.".xls";
	$name = $filename.".xls";
	$objWriter->save($file);
	
	redirect(base_url().$file);
}
?> 
